package com.swak.asm_test.invoker;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * 这种方式使用不正确
 * 
 * @author lifeng
 */
public class JdkProxyInvoker {

	private final Object proxy;
	
	public JdkProxyInvoker(Class<?> serviceType, Object service) {
		this.proxy = Proxy.newProxyInstance(serviceType.getClassLoader(), // 加载接口的类加载器
				new Class[] {serviceType}, // 一组接口
				new HWInvocationHandler(service)); // 自定义的InvocationHandler
	}
	
	@SuppressWarnings("unchecked")
	public <T> T getProxy() {
		return (T) proxy;
	}
}

/**
 * 简单的拦截器逻辑
 * @author lifeng
 */
class HWInvocationHandler implements InvocationHandler {
	private Object target;
	public HWInvocationHandler(Object target) {
		this.target = target;
	}
	public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
		return method.invoke(target, args);
	}
}